<?php
/**
 * rename
 * written in PHP-Gtk2
 * @author etongchina <etongchina@gmail.com>
 * @version 0.1
 */

require_once("PEAR/Config.php");

class Rename extends GtkWindow {
	public $img_dir;
	public $width;
	public $height;
	public $worm_pos;
	public $x,$y;
	public $worm_center;
	public $worm_length;
	public $wormhead;
	public $run;
	public $food;
	public $speed;
	public $direction; //0=up,1=right,2=down,3=left
	public $break_loop;

	public function __construct() {
		parent::__construct();

		$this->img_dir = PEAR_Config::singleton()->get('data_dir') . DIRECTORY_SEPARATOR . 'Game_Snake/';
		//$this->img_dir = "";

		$this->break_loop = false;
		$this->width = 499;
		$this->height = 495;
		$this->x = 0;
		$this->y = -5;
		$this->wormhead;
		$this->worm_center = 3;
		$this->worm_length = 40;
		$this->speed = 50000;
		$this->direction = 0;
		$this->run = false;
		$this->kill_loop = false;
		
		$this->set_size_request($this->width, $this->height);
		$this->buildGui();
		$this->show_all();
		
		
		$this->do_loop();
	}
	
	function do_loop($do_break = 0) {
		while(!$this->kill_loop) {
			while(gtk::events_pending()) {
				gtk::main_iteration_do(0);
			}
			if($this->run) {
				$this->move();
			}
			usleep($this->speed);
			if($do_break) break;
		}
	}
	
	public function death() {
		$this->kill_loop = true;
		$this->run = false;
		$this->btnReset->set_sensitive(true);
		echo "dead!\n";
	}

	public function move() {
		
		if(count($this->wormhead) < $this->worm_length) {
			$worm_head = GdkPixbuf::new_from_file($this->img_dir . 'pallet.png');
			$worm_head = $worm_head->subpixbuf(0,0,5,5);
			$worm_head = GtkImage::new_from_pixbuf($worm_head);
			$this->fixedMain->put($worm_head,$this->worm_pos[0]+=$this->x,$this->worm_pos[1]+=$this->y);
			$this->wormhead[] = array('img' => $worm_head,'x' => $this->worm_pos[0], 'y' => $this->worm_pos[1]);
		}else{
			$this->wormhead[0]['img']->destroy();
			array_shift($this->wormhead);
			$worm_head = GdkPixbuf::new_from_file($this->img_dir . 'pallet.png');
			$worm_head = $worm_head->subpixbuf(0,0,5,5);
			$worm_head = GtkImage::new_from_pixbuf($worm_head);
			$this->fixedMain->put($worm_head,$this->worm_pos[0]+=$this->x,$this->worm_pos[1]+=$this->y);
			$this->wormhead[] = array('img' => $worm_head,'x' => $this->worm_pos[0], 'y' => $this->worm_pos[1]);
		}
		
		$this->show_all();
		
		//check to see if hit self
		for($i=0;$i<count($this->wormhead)-1;$i++) {
			if($this->worm_pos[0] == $this->wormhead[$i]['x'] && $this->worm_pos[1] == $this->wormhead[$i]['y']) {
				$this->death();
			}
		}
		
		//check to see if hit sides
		if($this->worm_pos[0] < $this->worm_center-1 || $this->worm_pos[0] > $this->width - $this->worm_center-1) {
			$this->death();
		}elseif($this->worm_pos[1] < 25 + $this->worm_center-1 || $this->worm_pos[1] > $this->height - $this->worm_center-1) {
			$this->death();
		}
		
		//check to see if hit food
		for($i=0;$i<count($this->food);$i++) {
			if($this->worm_pos[0] == $this->food[$i]['x'] && $this->worm_pos[1] == $this->food[$i]['y']) {
				$this->food[$i]['img']->destroy();
				array_splice($this->food,$i);
				
				$this->worm_length+=5;
				if($this->speed >= 500) {
					$this->speed-=500;
				}
				
				$food = GdkPixbuf::new_from_file($this->img_dir . 'pallet.png');
				$food = $food->subpixbuf(6,0,5,5);
				$food = GtkImage::new_from_pixbuf($food);
				
				$x_food = ($this->worm_center-1)+(rand(1,$this->width/5)-1)*($this->worm_center+2);
				$y_food = ($this->worm_center-1)+25+(rand(1,($this->height-25)/5)-2)*($this->worm_center+2);
				$this->food[] = array('img' => $food, 'x'=>$x_food, 'y'=> $y_food);

				$this->fixedMain->put($food,$x_food,$y_food);
				
				$this->score_num++;
				$this->score->set_label(str_pad($this->score_num,4,"0",STR_PAD_LEFT));
				break;
			}
		}
	}
	
	public function buildGUI() {
		$this->set_title('Snake');
		$this->set_resizable(false);
		$this->connect_simple('destroy', array($this, 'close'));
		$this->set_position(Gtk::WIN_POS_CENTER_ALWAYS);

		$vBoxMain = new GtkVBox();
		$this->add($vBoxMain);

		$EventBox = new GtkEventBox();
		$this->eventBox = $EventBox;
		$vBoxMain->add($EventBox);

		$FixedMain = new GtkFixed();
		$this->fixedMain = $FixedMain;
		$EventBox->add($FixedMain);
		
		$this->score = new GtkLabel("0000");
		$this->score_num = 0;
		$FixedMain->put($this->score,0,0);

		$background = GtkImage::new_from_file($this->img_dir .'background.png');
		$FixedMain->put($background,0,25);
		
		
		$worm_head = GdkPixbuf::new_from_file($this->img_dir . 'pallet.png');
		$worm_head = $worm_head->subpixbuf(0,0,5,5);
		$worm_head = GtkImage::new_from_pixbuf($worm_head);
		$this->worm_pos = array(($this->worm_center-1)+50*($this->worm_center+2),($this->worm_center-1)+20+50*($this->worm_center+2));
		$this->wormhead[] = array('img' => $worm_head, 'x'=> $this->worm_pos[0], 'y'=> $this->worm_pos[1]);
		
		
		$FixedMain->put($worm_head,$this->worm_pos[0],$this->worm_pos[1]);
		
		
		$food = GdkPixbuf::new_from_file($this->img_dir . 'pallet.png');
		$food = $food->subpixbuf(6,0,5,5);
		$food = GtkImage::new_from_pixbuf($food);
		$x_food = ($this->worm_center-1)+(rand(1,$this->width/5)-1)*($this->worm_center+2);//
		$y_food = ($this->worm_center-1)+25+(rand(1,($this->height-25)/5)-1)*($this->worm_center+2);
		$this->food[] = array('img' => $food, 'x'=>$x_food, 'y'=> $y_food);
		
		$FixedMain->put($food,$x_food,$y_food);
		
		$this->btnReset = new GtkButton("reset");
		$this->btnReset->set_sensitive(false);
		$this->btnReset->connect_simple("clicked",array($this,"reset"));
		$FixedMain->put($this->btnReset,250,0);
		
		$this->connect("key-press-event",array($this,"on_key"));
	}
	
	function reset() {
		
		foreach($this->wormhead AS $index => $value) {
			$value['img']->destroy();
		}
		$this->wormhead = null;
		
		foreach($this->food AS $index => $value) {
			$value['img']->destroy();
		}
		$this->food = null;
		
		$worm_head = GdkPixbuf::new_from_file($this->img_dir . 'pallet.png');
		$worm_head = $worm_head->subpixbuf(0,0,5,5);
		$worm_head = GtkImage::new_from_pixbuf($worm_head);
		$this->worm_pos = array(($this->worm_center-1)+50*($this->worm_center+2),($this->worm_center-1)+20+50*($this->worm_center+2));
		$this->wormhead[] = array('img' => $worm_head, 'x'=> $this->worm_pos[0], 'y'=> $this->worm_pos[1]);
		
		
		$this->fixedMain->put($worm_head,$this->worm_pos[0],$this->worm_pos[1]);
		
		
		$food = GdkPixbuf::new_from_file($this->img_dir . 'pallet.png');
		$food = $food->subpixbuf(6,0,5,5);
		$food = GtkImage::new_from_pixbuf($food);
		$x_food = ($this->worm_center-1)+(rand(1,$this->width/5)-1)*($this->worm_center+2);//
		$y_food = ($this->worm_center-1)+20+(rand(1,($this->height-20)/5)-1)*($this->worm_center+2);
		$this->food[] = array('img' => $food, 'x'=>$x_food, 'y'=> $y_food);
		
		$this->fixedMain->put($food,$x_food,$y_food);
		
		$this->score_num = 0;
		$this->score->set_label(str_pad($this->score_num,4,"0",STR_PAD_LEFT));
		
		$this->btnReset->set_sensitive(false);
		
		$this->x = 0;
		$this->y = -5;
		
		$this->break_loop = false;
		$this->kill_loop = false;
		$this->run = false;

		$this->show_all();
		
		$this->do_loop();
	}
	
	function on_key($widget, $event) {
		if(!$this->break_loop) {
			if($this->run) {
				if($event->keyval == 65362 && $this->direction != 2) {      // UP
					$this->x = 0;
					$this->y = -5;
					$this->direction = 0;
				}else if($event->keyval == 65364 && $this->direction != 0) { // DOWN
					$this->x = 0;
					$this->y = 5;
					$this->direction = 2;
				}else if($event->keyval == 65361 && $this->direction != 1) { // LEFT
					$this->x = -5;
					$this->y = 0;
					$this->direction = 3;
				}else if($event->keyval == 65363 && $this->direction != 3) { // RIGHT
					$this->x = 5;
					$this->y = 0;
					$this->direction = 1;
				}else if ($event->keyval == 65307) { //ESC
					$this->death();
				}
			}
			$this->run = true;
		}
	}
	
	function close() {
		$this->kill_loop = true;
		gtk::main_quit();
	}



}//class Game_Snake extends GtkWindow
?>
